<?php

class Order extends Admincontroll{
    private $orderlogic;
    private $orderitemlogic;
     private $goodslogic;
    public function __construct() {
        parent::__construct();
        $this->orderlogic=  $this->load("order");
        $this->orderitemlogic=$this->load("order_item");
        $this->goodslogic=  $this->load("goods");
    }
    
    

    //后台默认首页
    public function index(){
        $where="status=1";
        if(isset($_REQUEST["search"])){
           $order_no=  isset($_REQUEST["order_no"])?$_REQUEST["order_no"]:"";
        
           if($order_no){
               $where.=" and order_no like '%{$order_no}%' ";
           }
           
           $user_name=  isset($_REQUEST["user_name"])?$_REQUEST["user_name"]:"";
        
           if($user_name){
               $where.=" and user_name like '%{$user_name}%' ";
           }
           
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        $page=  isset($_REQUEST["page"])?intval($_REQUEST["page"]):1;
        $count=  $this->orderlogic->get_count($where);
        $list=$this->orderlogic->select_page("*", $where, $page,20,"order by id desc");
        $page_data = Common::getPages($count, $page, 20);
        Temp::assign("page_data", $page_data);
        Temp::assign("list", $list);
        Temp::display();
    }
    public function edit(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
        if(isset($_REQUEST["submit"])){
            
        }else{
            $order=$this->orderlogic->get_one("*", "id={$id}");
            $order_item=$this->orderitemlogic->select("*", "order_id={$id}");
            Temp::assign("order", $order);
            Temp::assign("order_item", $order_item);
        }
        
        Temp::assign("id", $id);
        Temp::display();
    }
    //重新计算订单表的商品销售金额，商品总重
    private function jisuan_order($order_id){
        $db=$this->orderitemlogic->get_one("sum(sale_weight) as sale_weight,sum(sale_price*sale_weight) as sale_price","order_id={$order_id}");
        $this->orderlogic->update($db,"id={$order_id}");
    }
    //重新计算订单下商品的销量和历史总重
    private function jisuan_order_goods_saleinfo($order_id){
        $order_item=$this->orderitemlogic->select("*","order_id={$order_id}");
        foreach($order_item as $item){
            $this->jisuan_ordernum($item["good_id"]);
            $this->jisuan_sale_weight($item["good_id"]);
        }
    }
    //计算商品表的商品销量
    private function jisuan_ordernum($goods_id){
        $db=$this->orderitemlogic->get_one("count(order_id) as order_num", "good_id={$goods_id}");
        $this->goodslogic->update($db,"id={$goods_id}");
    }
    //计算商品表的历史总重量
    private function jisuan_sale_weight($goods_id){
        $db=$this->orderitemlogic->get_one("sum(sale_weight) as sale_weight", "good_id={$goods_id}");
        $this->goodslogic->update($db,"id={$goods_id}");
    }
    //ajax订单作废操作
    
    //ajax删除订单明细
    public function public_ajax_delete_order_item(){
         $msg=array();
         $vid=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
         if($vid){
             $order_item=  $this->orderitemlogic->get_one("*","id={$vid}");
             $this->orderitemlogic->delete("id={$vid}");
             $this->jisuan_order($order_item["order_id"]);
             $this->goodslogic->update("order_num=order_num-1,sale_weight=sale_weight-{$order_item["sale_weight"]}","id={$order_item["good_id"]}");
             
              $msg["status"]=1; 
         }else{
             $msg["status"]=-1; 
         }
          echo json_encode($msg);
    }
    
    public function public_ajax_zuofei(){
     $oid=isset($_REQUEST["oid"])?intval($_REQUEST["oid"]):0;
     $msg=array();
     if($oid){
         $orderdata=array("status"=>0);
         $this->orderlogic->update($orderdata,"id={$oid}");
         $msg["status"]=1;
     }else{
         $msg["status"]=-1;
     }
     echo json_encode($msg);
}
    //ajax更新订单明细
    public function public_ajax_update_order_item(){
        $msg=array();
        $vid=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        $sale_price=isset($_REQUEST["sale_price"])?$_REQUEST["sale_price"]:"";
        $sale_weight=isset($_REQUEST["sale_weight"])?$_REQUEST["sale_weight"]:"";
        if($vid){
            $order_item=  $this->orderitemlogic->get_one("*","id={$vid}");
            if($sale_price){
                $data=array(
                    "sale_price"=>$sale_price
                );
                $this->orderitemlogic->update($data,"id={$vid}");
            }
            if($sale_weight){
                $data=array(
                    "sale_weight"=>$sale_weight
                );
                $this->orderitemlogic->update($data,"id={$vid}");
            }
            
            $this->jisuan_order($order_item["order_id"]);
            $this->jisuan_order_goods_saleinfo($order_item["order_id"]);
            $msg["status"]=1;
        }else{
            $msg["status"]=-1; 
        }
        echo json_encode($msg);
    }
}
